<?php

require_once("../include/MySugarApplication.php");
require_once("../include/Contact.php");
session_start();
$app->checkSession('Contacts/Save.php', $server_unique_key);

$id = $_REQUEST["id"];
$moduleName = $component->moduleName;

$actionType = isset($_REQUEST["actionType"]) ? $_REQUEST["actionType"] : "";

if ($actionType == "DELETE") {

	$result=$component->deleteRecord($id);

} else {

	$errors = array();


	if ($actionType == "UPDATE" && $id == "") {
		$errros["bitzer-global"] = "Cannot update when primary key is not passed";
	} else  {
		if (isset($_FILES["photo_c"])) {
			$photoDirPath = "/".$app_name."/photos/";
			if ($_FILES["photo_c"]["name"] != "" ) {
				if ($_FILES["photo_c"]["error"] > 0) {
 					$errors["photo"] = "File upload error: " . $_FILES["file"]["error"];
 				} else {
  					$target_path = $_SERVER['DOCUMENT_ROOT'].$photoDirPath.$_FILES["photo_c"]["name"];
  					if(move_uploaded_file($_FILES['photo_c']['tmp_name'], $target_path)) {
  						$photo_c = $photoDirPath.$_FILES["photo_c"]["name"];;
					} else{
						$errors["photo"] = "There was an error uploading the file, please try again!";
					}
				}
 			} else {

				/** Delete file **/
				$photo_c = "";
				$query=' contacts.id = "'.$id.'"';
				
        			$rows = $component->getRecord($query);
				foreach($rows as $record) {
                			$previous_photo_c = $record["photo_c"];

				}
				if ($previous_photo_c != "") {
					$file = basename($previous_photo_c);
  					$file = $_SERVER['DOCUMENT_ROOT'].$photoDirPath.$file;
					unlink($file);
				}
			}
		} else if (isset($_REQUEST["photo_c"])) {

			/** Delete file **/
                        $photo_c = "";
                        $query=' contacts.id = "'.$id.'"';
                                
                        $rows = $component->getRecord($query);
                        foreach($rows as $record) {
                        	$previous_photo_c = $record["photo_c"];
                        }
                        
			if ($previous_photo_c != "") {
                        	$file = basename($previous_photo_c);
                                $file = $_SERVER['DOCUMENT_ROOT'].$photoDirPath.$file;
                                unlink($file);
                        }


		} 


		$salutation = $_REQUEST["salutation"];

		if ($salutation == "")
			$errors["salutation"] = "Salutation is mandatory";

		$first_name = $_REQUEST["first_name"];
		if ($first_name == "")
			$errors["first_name"] = "First name is mandatory";

		$last_name = $_REQUEST["last_name"];
		if ($last_name == "")
			$errors["last_name"] = "Last name is mandatory";
	
		$account_id = $_REQUEST["account_id"];
		$lead_source = $_REQUEST["lead_source"];
		$campaign_id = $_REQUEST["compaign_id"];
		$title = $_REQUEST["title"];
		$department = $_REQUEST["department"];
		$assigned_user_id = $_REQUEST["assigned_user_id"];
		$email1 = $_REQUEST["email1"];
		$phone_work = $_REQUEST["phone_work"];
		$phone_mobile = $_REQUEST["phone_mobile"];
		$phone_home = $_REQUEST["phone_home"];
		$phone_other = $_REQUEST["phone_other"];
		$phone_fax = $_REQUEST["phone_fax"];
		$do_not_call = $_REQUEST["do_not_call"];
		$primary_address_street = $_REQUEST["primary_address_street"];
		$primary_address_city = $_REQUEST["primary_address_city"];
		$primary_address_state = $_REQUEST["primary_address_state"];
		$primary_address_postalcode = $_REQUEST["primary_address_postalcode"];
		$primary_address_country = $_REQUEST["primary_address_country"];
		$description = $_REQUEST["description"];
		$date_modified = $_REQUEST["last_modified_on"];

		if ($lead_source != "" && $account_id == "")
			$errors["account_id"] = "Account required when Lead Source is specified";
	



		if (count($errors) == 0) {
			$result=$component->saveRecord($id, $photo_c, $salutation, $first_name, $last_name,
 								  	  $account_id, $lead_source, $campaign_id,
								      $title, $department, $assigned_user_id,
								  	$email1, $phone_work, $phone_mobile, $phone_home, $phone_other,
								  	$phone_fax, $do_not_call, $primary_address_street,
								  	$primary_address_city, $primary_address_state,
								  	$primary_address_postalcode, 
								  	$primary_address_country, $description,
								  	$date_modified);
		} else {
			error_log(" errors found!!!");
		}

	}
}

include("../include/SaveCommon.php");

?>
